/*
  Copyright 2006 by Sean Luke
  Licensed under the Academic Free License version 3.0
  See the file "LICENSE" for more information
 */

package ec.app.deferEvalCoevolve1;

import ec.*;
import ec.simple.*;
import ec.coevolve.*;
import ec.vector.DoubleVectorIndividual;

/**
 * @author RothmD01
 *
 */
public class CoevoRosenVar2 extends Problem implements SimpleProblemForm, GroupedProblemForm {

	double rosenbrock(double i, double j) {
		return -(100.0d * (i - j * j) * (i - j * j) + (1.0d - j) * (1.0d - j));
	}
	
	double generalRosenbrock( double x[] ) {
		double retVal = 0.0;
		
		for( int i = 0; i < x.length-1; i++ ) {
			retVal += rosenbrock(x[i], x[i+1]);
		}
		return retVal;
	}
	
	// denying absolute evaluation during evaluation - need to defer until
	// reproduction and elite selction
	// TODO drr pulled absolute fitness
	public void preprocessPopulation(final EvolutionState state, Population pop) {
		for (int i = 0; i < pop.subpops.length; i++)
			for (int j = 0; j < pop.subpops[i].individuals.length; j++)
				((SimpleFitnessVar2) (pop.subpops[i].individuals[j].fitness))
				.setFitness(state, -1000000000, false);
		return;
	}

	// denying absolute evaluation during evaluation - need to defer until
	// reproduction and elite selction
	// TODO drr pulled absolute fitness
	public void postprocessPopulation(final EvolutionState state, Population pop) {

		return;
	}

	// denying absolute evaluation during evaluation - need to defer until
	// reproduction and elite selection
	// TODO drr pulled absolute fitness
	public void evaluate(final EvolutionState state, final Individual[] ind, // the individuals to evaluate together
			final boolean[] updateFitness, // should this individuals' fitness be updated?
			final boolean countVictoriesOnly, // can be neglected in cooperative coevolution - 100429 drr DISAGREE
			int[] subpops, final int threadnum) {

		return;
	}
	
	// TODO drr want to modify this for it to act on a subset of the target population, and eventually enhance for some sort of preferential team mate selection	
	/**
	 * chooses a random individual from a target subpopulation
	 * @param state
	 * @param lead
	 * @param subPop
	 * @param pop
	 * @return
	 */
	private Individual chooseInd( EvolutionState state, Individual lead, int subPop, Population pop ) {
		Individual retVal = null;
		int randomIndex;
		randomIndex = state.random[subPop].nextInt( pop.subpops[subPop].individuals.length );
		retVal = pop.subpops[subPop].individuals[randomIndex];
		return retVal;
	}

	@Override
	public void describe(Individual ind, EvolutionState state,
			int subpopulation, int threadnum, int log, int verbosity) {
//		System.err.println( "Method CoevoRosenVar2.java:describe currently unimplemented");
		// TODO Auto-generated method stub
		
	}

	@Override
	public void evaluate(EvolutionState state, Individual ind,
			int subpopulation, int threadnum) {
//		System.err.println( "Method CoevoRosenVar2.java:evaluate currently unimplemented");
		// TODO Auto-generated method stub
		
	}

}
